package com.ktgj.customer.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.constant.InsuranceTypeEnum;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.common.core.utils.RSAUtils;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.core.utils.xml.XmlUtils;
import com.ktgj.customer.common.AxInsuranceUtils;
import com.ktgj.customer.common.DxInsuranceUtils;
import com.ktgj.customer.domain.*;

import com.ktgj.customer.domain.Insurance.axModel.*;
import com.ktgj.customer.domain.Insurance.dxModel.*;
import com.ktgj.customer.domain.vo.ProductVo;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.IInsuranceRecordService;
import org.apache.commons.codec.digest.DigestUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.service.ICustomerRightdetailService;

import javax.xml.bind.annotation.XmlElement;

/**
 * 会员权益Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-30
 */
@Service
public class CustomerRightdetailServiceImpl implements ICustomerRightdetailService {
    @Autowired
    private CustomerRightdetailMapper customerRightdetailMapper;
    @Autowired
    private CustomerCardMapper customerCardMapper;
    @Autowired
    private GuaranteeMapper guaranteeMapper;
    @Autowired
    private InsuranceTypeMapper insuranceTypeMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private IInsuranceRecordService insuranceRecordService;
    @Autowired
    private CouponMapper couponMapper;

    /**
     * 查询会员权益
     *
     * @param icustomerRightdetailD 会员权益ID
     * @return 会员权益
     */
    @Override
    public CustomerRightdetail selectCustomerRightdetailById(Long icustomerRightdetailD) {
        return customerRightdetailMapper.selectCustomerRightdetailById(icustomerRightdetailD);
    }

    /**
     * 查询会员权益列表
     *
     * @param customerRightdetail 会员权益
     * @return 会员权益
     */
    @Override
    public List<CustomerRightdetail> selectCustomerRightdetailList(CustomerRightdetail customerRightdetail) {
        return customerRightdetailMapper.selectCustomerRightdetailList(customerRightdetail);
    }

    /**
     * 新增会员权益
     *
     * @param customerRightdetail 会员权益
     * @return 结果
     */
    @Override
    public int insertCustomerRightdetail(CustomerRightdetail customerRightdetail) {
        customerRightdetail.setCreateTime(DateUtils.getNowDate());
        return customerRightdetailMapper.insertCustomerRightdetail(customerRightdetail);
    }

    /**
     * 修改会员权益
     *
     * @param customerRightdetail 会员权益
     * @return 结果
     */
    @Override
    public int updateCustomerRightdetail(CustomerRightdetail customerRightdetail) {
        customerRightdetail.setUpdateTime(DateUtils.getNowDate());
        return customerRightdetailMapper.updateCustomerRightdetail(customerRightdetail);
    }

    /**
     * 批量删除会员权益
     *
     * @param icustomerRightdetailDs 需要删除的会员权益ID
     * @return 结果
     */
    @Override
    public int deleteCustomerRightdetailByIds(Long[] icustomerRightdetailDs) {
        return customerRightdetailMapper.deleteCustomerRightdetailByIds(icustomerRightdetailDs);
    }

    /**
     * 删除会员权益信息
     *
     * @param icustomerRightdetailD 会员权益ID
     * @return 结果
     */
    @Override
    public int deleteCustomerRightdetailById(Long icustomerRightdetailD) {
        return customerRightdetailMapper.deleteCustomerRightdetailById(icustomerRightdetailD);
    }

    /**
     * 批量添加会员权益
     *
     * @param customerRightdetails
     * @return
     */
    @Override
    public int insertBatch(List<CustomerRightdetail> customerRightdetails) {
        return customerRightdetailMapper.insertBatch(customerRightdetails);
    }

    @Override
    //实例化会员权益
    public int addCustomerRightdetail(List<Rightdetail> rightdetailList, Long customerId, Date validDate, Date inValidDate, Long employeeId, String rightDetailSource, Long customerCardId, Long orderSubId, String cardCode, String payType, String orderId) {
        int count = 0;
        //(权益集合， 会员id， 生效时间， 到期时间， 员工id， 权益来源（卡、权益包）， 会员卡id， 子订单id)

        List<CustomerRightdetail> customerRightdetails = new ArrayList<>();
        for (Rightdetail itemRd : rightdetailList) {
            for (int i = 0; i < itemRd.getNumberOfTimes(); i++) {
                    CustomerRightdetail customerRightdetail = new CustomerRightdetail();
                    customerRightdetail.setCustomerId(customerId);//会员id
                    customerRightdetail.setRightdetailId(itemRd.getRightdetailId());//权益id
                    customerRightdetail.setRightdetailName(itemRd.getRightdetalName());//权益名称
                    customerRightdetail.setCouponMoney(itemRd.getCouponMoney());//券金额
                    customerRightdetail.setValidDate(validDate);//生效日期
                    customerRightdetail.setInvalidDate(inValidDate);//失效日期
                    customerRightdetail.setCreateEmployeeId(employeeId);//创建人
                    customerRightdetail.setCreateTime(DateUtils.getNowDate());//创建时间
                    customerRightdetail.setRightdetailSource(rightDetailSource);//权益来源（卡、权益包）
                    customerRightdetail.setCustomerCardId(customerCardId);//会员卡id
                    customerRightdetail.setOrderSubId(orderSubId);//子订单id
                    customerRightdetail.setRightdetailType(itemRd.getRightdetailType());//权益类型
                    customerRightdetail.setRightdetailTypeid(itemRd.getRightdetailTypeid());//权益类型id
                    customerRightdetail.setRightdetailState(CustomerConstants.unexecuted.getValue());//权益状态
                    customerRightdetails.add(customerRightdetail);
                if (CustomerConstants.rightTypeInsurance.getValue().equals(itemRd.getRightdetailType())) {
                    ProductVo productVo = new ProductVo();
                    productVo.customerId = customerId;
                    productVo.insuranceTypeId = itemRd.getRightdetailTypeid();
                    productVo.orderNum = orderId;
                    productVo.orderSource = "customer";
                    productVo.relation = "01";
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    productVo.insuranceStartDate = simpleDateFormat.format(new Date());
                    productVo.cardNum = cardCode;
                    productVo.paymentChannel = payType;

                    try {
                        insuranceRecordService.insertInsuranceRecord(productVo.customerId, productVo);
                    } catch (Exception e){
                        System.err.println("添加保险出错");
                        e.printStackTrace();
                        return 0;
                    }
                }
            }
        }
        if (customerRightdetails.size() > 0) {//会员权益加入会员权益表
            count = customerRightdetailMapper.insertBatch(customerRightdetails);/** 实例化卡权益 */
        }
//        if (guaranteeList.size() > 0) {//保险
//            count = guaranteeMapper.insertGuaranteeList(guaranteeList);/** 实例化卡权益 */
//        }

        return count;
    }

    /**
     * 查询会员是否有贵宾厅无限券
     * */
    @Override
    public boolean infiniteVIPHall(Long id) {
        Coupon coupon = couponMapper.infiniteVIPHall();
        if (coupon != null){
            CustomerRightdetail customerRightdetail = customerRightdetailMapper.infiniteVIPHall(id, coupon.getCouponId());
            if (customerRightdetail != null){
                return true;
            }
        }
        return false;
    }

}
